package com.wing.cms.tag;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyTagSupport;

import com.wing.cms.domain.Channel;
import com.wing.cms.domain.Lookfeel;
import com.wing.cms.service.ChannelManager;
import com.wing.cms.service.LookfeelManager;
import com.wing.common.Constants;
import com.wing.common.services.HtmlStaticTemplateService;
import com.wing.common.util.RequestUtils;
import com.wing.common.util.SpringDI;
import com.wing.portal.aggregation.Fragment;
import com.wing.portal.aggregation.PlaceholderFragment;
import com.wing.sys.service.AuthManager;
import com.wing.sys.service.UserManager;

/**
 * 
 * 
 * @author wyf
 * 
 */
public class PlaceholderTag extends BodyTagSupport {
	
	protected String placeholder = null;

	public String getPlaceholder() {
		return placeholder;
	}

	public void setPlaceholder(String placeholder) {
		this.placeholder = placeholder;
	}

	/**
	 * Process the start of this tag.
	 * 
	 * @exception JspException
	 *                if a JSP exception has occurred
	 */
	public int doStartTag() throws JspException {
		return (2);
	}

	/**
	 * Save the associated label from the body content (if any).
	 * 
	 * @exception JspException
	 *                if a JSP exception has occurred
	 */
	public int doAfterBody() throws JspException {
		return (SKIP_BODY);
	}

	/**
	 * Release any acquired resources.
	 */
	public void release() {
		super.release();
		placeholder = null;
	}

	/**
	 * Process the end of this tag.
	 * <p>
	 * Support for indexed property since Struts 1.1
	 * 
	 * @exception JspException
	 *                if a JSP exception has occurred
	 */
	public int doEndTag() throws JspException {
		String userId = (String)pageContext.getSession().getAttribute(Constants.SESSION_USER_ID);
		String sitePath = RequestUtils.getSitePath(pageContext.getSession());
		String siteId = (String)pageContext.getSession().getAttribute(Constants.SESSION_SITE_ID);
		
		String pageId = (String)pageContext.getRequest().getAttribute("channelId");
		String s = (String)pageContext.getRequest().getAttribute("s");
		String cd = (String)pageContext.getRequest().getAttribute("cd");
		String isLayout = (String)pageContext.getRequest().getParameter("isLayout");
		if(isLayout == null || isLayout.equals(""))isLayout = "0";
		if (pageId == null || pageId.equals("") || this.placeholder == null
				|| this.placeholder.equals(""))
			throw new JspException("缺少频道参数,无法渲染该块");
		ChannelManager channelManager = (ChannelManager) SpringDI.getBean("channelManager");
		LookfeelManager lookfeelManager = (LookfeelManager) SpringDI.getBean("lookfeelManager");
		String lookfeel = "default";
		if(pageId.indexOf("a,")!= -1){
			pageId = pageId.substring(0, pageId.indexOf(","))+","+siteId;
		}else if(pageId.indexOf("p,")!= -1){
			pageId = pageId.substring(0, pageId.indexOf(","))+","+siteId;
		}else{
			Channel c = channelManager.findById(pageId);
			if(c.getLookandfeel()!=null){
				Lookfeel look = lookfeelManager.findById(c.getLookandfeel());
				lookfeel = look.getFilename();
			}
		}
		Map<String,String> params = new HashMap<String,String>();
		params.put("pageId",pageId );
		params.put("s",s );
		params.put("cd",cd );
		params.put("placeholder",placeholder);
		params.put("userId",userId);
		params.put("isLayout",isLayout);
		params.put("sitePath", sitePath);
		params.put("resourcePath",pageContext.getServletContext().getContextPath()+"/site/"+sitePath+"/resources/"+lookfeel);
		params.put("contextPath",pageContext.getServletContext().getContextPath());
		params.put("siteId", siteId);
		
		Fragment fragment = new PlaceholderFragment(params);
		try {
			if(isLayout.equals("1")){
				//判断是不是超级管理员，或者站点管理员
				AuthManager authManager = (AuthManager) SpringDI.getBean("authManager");
				UserManager usermanager = (UserManager) SpringDI.getBean("userManager");
				if ((usermanager.isAdminByUserId(userId)||authManager.isHasAuth("site",userId,siteId))&&isLayout.equals("1")) {
					String htmlBlock = HtmlStaticTemplateService
							.getHtml("configPortlet");
					String newHtmlBlock = htmlBlock.replaceAll("\\$hoderId",
							this.placeholder);
					String newHtmlBlock1 = newHtmlBlock.replaceAll("\\$channelId",
							"'" + pageId + "'");
					pageContext.getOut().write(newHtmlBlock1);
				}
				pageContext.getOut().write("<div id = \"column-"+this.placeholder+"\" class=\"column\">");
			}else{
				pageContext.getOut().write("<div id = \"column-"+this.placeholder+"\">");
			}


			pageContext.getOut().write(fragment.service());
			pageContext.getOut().write("</div>");
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		return (EVAL_PAGE);
	}
}
